<?php
/**
 * @license http://www.gnu.org/licenses/lgpl.html LGPL
 * @author Harlan Iverson <h dot iverson at gmail dot com>
 * @copyright Copyright (c) 2006, Harlan Iverson.
 * @version $Id: test-faces.php 20 2006-08-01 05:54:40Z Harlan $
 */
 
 
 

require_once 'HTML/Template/Smarty/Smarty.class.php';
require_once 'PhpFaces/ViewHandler.class.php';
require_once 'PhpFaces/FacesContext.class.php';
require_once 'PHPUnit.php';

class TestBean {
	private $accessorProperty;
	public $fieldProperty;
	public $nestedClassProperty;
	
	public function __construct() {
		$this->fieldProperty = "field property";
		$this->accessorProperty = "accessor property";
		$this->nestedClassProperty = new StdClass();
		$this->nestedClassProperty->fieldProperty = "nested field";
	}
	
	public function getAccessorProperty() {
		return $this->accessorProperty;
	}
	
	public function setAccessorProperty( $newValue ) {
		$this->accessorProperty = $newValue;
	}
	
	public function doSomeAction() {
		return "success";
	}
	
	public $listData = 
		array(
			array( "id" => "1", "prop1" => "one1", "prop2" => "two1" ),
			array( "id" => "2", "prop1" => "one2", "prop2" => "two2" ),
			array( "id" => "3", "prop1" => "one3", "prop2" => "two3" )
		);

};


class FacesContextResolverTest extends PHPUnit_TestCase {

	var $facesContext;

	function __construct($name) {
		$this->PHPUnit_TestCase($name);
	}
	
	function setUp() {
		$this->facesContext =& new PhpFaces_FacesContext();
		$this->facesContext->registerBean( "testBean", "TestBean", PhpFaces_FacesContext::SCOPE_REQUEST );
	}
	

	
	function testResolveBean() {
		$value = $this->facesContext->resolve( "#{testBean}" );
		
		$this->assertTrue( $value instanceof TestBean, "TestBean did not resolve to the correct type." );
	}
	
	function testResolveFieldProperty() {
		$value = $this->facesContext->resolve( "#{testBean.fieldProperty}" );
		
		$testBean = new TestBean();
		
		$this->assertEquals( $testBean->fieldProperty, $value );
	}
	
	function testResolveAccessorProperty() {
		$value = $this->facesContext->resolve( "#{testBean.accessorProperty}" );
		
		$testBean = new TestBean();
		
		$this->assertEquals( $testBean->getAccessorProperty(), $value );
	}
	
	function testResolveNestedFieldProperty() {
		$value = $this->facesContext->resolve( "#{testBean.nestedClassProperty.fieldProperty}" );
		
		$testBean = new TestBean();
		
		$this->assertEquals( $testBean->nestedClassProperty->fieldProperty, $value );
	}
	
	function testResolveAction() {
		$value = $this->facesContext->resolve( "#{testBean.doSomeAction}" );

		$testBean = new TestBean();

		$this->assertTrue( is_callable( $value ) );
		
		$this->assertEquals( $testBean->doSomeAction(), call_user_func($value) );
	}
	
	function testResolveString() {
		try {
			$valueToResolve = "stuff is neat";
			$value = $this->facesContext->resolve( $valueToResolve );
			$this->assertEquals( $valueToResolve, $value );
		}catch( Exception $e ) {
			$this->fail( "did not resolve plain string." );
		}
	}
	
	function testResolveGarbage() {
		try {
			$value = $this->facesContext->resolve( "#{testBean.fsdfs}" );
			$this->fail( "resolved garbage" );
		}catch( Exception $e ) {
			// pass
		}
	}
	
	
	function testGetRequestName() {
		$name = $this->facesContext->getRequestName( "#{someBean.someProperty}" );
		$this->assertEquals( "faces-someBean__someProperty", $name );
		
		$name = $this->facesContext->getRequestName( "#{someBean.nestedClass.someProperty}" );
		$this->assertEquals( "faces-someBean__nestedClass__someProperty", $name );
	}
	
	function testGetActionName() {
		$name = $this->facesContext->getActionName( "#{someBean.someAction}" );
		$this->assertEquals( "action-someBean__someAction", $name );
		
		$name = $this->facesContext->getActionName( "#{someBean.nestedClass.someAction}" );
		$this->assertEquals( "action-someBean__nestedClass__someAction", $name );
	}

	

}



?>